<?php
			// Use the MiniXML library for easy XML editing
			require_once('minixml/minixml.inc.php');
			include "config.php";
			//sanitize input, prevent folder transversals
			//add base url
			$path = $_GET["path"];
			
			
			 
			$result_image_id = mysql_query("SELECT * from images where file_path='$path'");
				while($row = mysql_fetch_array($result_image_id))
					{		//should ever only return one userid.
							$image_id = $row[id];
							
					}
			$result = mysql_query("SELECT * FROM annotations where image_id='$image_id'");
							
			//Make SQL result $row[file_path]
			while($row = mysql_fetch_array($result)){
				$string .=  $row[file_path].",";

			}
			$string = substr($string, 0, -1); //get rid of the tailing comma!
			$layerArray = split("," , $string);

			
			$outputString .= '<svg xmlns="http://www.w3.org/2000/svg" width="640" height="480" xmlns:xlink="http://www.w3.org/1999/xlink" onload="RunScript(evt)" style="background: #e8e6de">';
			//script to scall all elements of svg
			$outputString .= '<script type="text/ecmascript"><![CDATA[var g_elements = new Array();var SVGDoc;var SVGRoot;function RunScript(LoadEvent) {top.SVGsetDimension=setDimension;top.SVGsetScale=setScale;SVGDoc=LoadEvent.target.ownerDocument;g_elements=SVGDoc.getElementsByTagName("g");}function setDimension(w,h) {SVGDoc.documentElement.setAttribute("width", w);SVGDoc.documentElement.setAttribute("height", h);}function setScale(sw, sh) {for (i=0;i<g_elements.length;i++){g_elements[i].setAttribute("transform", "scale(" + sw + " " + sh +")");}}]]></script>';
			$outputString .= '<script type="text/ecmascript"><![CDATA[function toggleLayers(evt,selectedLayer){var g_elements = new Array();var shapes = new Array();var SVGDoc;var SVGRoot;SVGDoc=evt.target.ownerDocument;g_elements=SVGDoc.getElementsByTagName("g");shapes=SVGDoc.getElementsByClassName("annotation");for (i=0;i<shapes.length;i++){if(shapes[i].getAttribute("fill-opacity")==1||shapes[i].getAttribute("fill-opacity")==null){shapes[i].setAttribute("fill-opacity",0);shapes[i].setAttribute("stroke-opacity",1);}else{shapes[i].setAttribute("fill-opacity",1);shapes[i].setAttribute("stroke-opacity",1);}}for (i=0;i<g_elements.length;i++){if(g_elements[i].getAttribute("display")=="none"){g_elements[i].setAttribute("display", "inline");}else{if(g_elements[i]!=selectedLayer){g_elements[i].setAttribute("display", "none");}}}}]]></script>';
			$outputString .= '<image xlink:href="'.$path.'" width="100%" height="100%" preserveAspectRatio="xMinYMin"/>';

			for ($i=0; $i<count($layerArray);$i++){
				$parsedDoc = new MiniXMLDoc();
				$parsedDoc->fromFile($layerArray[$i]);
				$rootEl = $parsedDoc->getRoot();
				$layer =& $rootEl->getElement('g');
				$outputString .= $layer->toString();

			}
			$outputString .= '</svg>';

			$savedSVG = $outputString;
			
			header('Content-type: image/svg+xml');
			echo $savedSVG;		
			?>


	
